package au.edu.qut.bpm.genbinder.genetic.mutation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.uncommons.watchmaker.framework.EvolutionaryOperator;

import au.edu.qut.bpm.genbinder.genetic.genesis.LogGreedyFactory;
import au.edu.qut.bpm.genbinder.models.ConcreteEventLog;

public class LogNewGreedySolution implements EvolutionaryOperator<ConcreteEventLog> {
	private boolean performSafetyCheck = true;
	private final double mutChance;

	public LogNewGreedySolution(boolean performSafetyCheck) {
		this(performSafetyCheck, 0.05);
	}

	public LogNewGreedySolution(boolean performSafetyCheck, double d) {
		this.performSafetyCheck = performSafetyCheck;
		mutChance = d;
	}

	public List<ConcreteEventLog> apply(List<ConcreteEventLog> selectedCandidates, Random rng) {
		List<ConcreteEventLog> mutatedCandidates = new ArrayList<ConcreteEventLog>(selectedCandidates.size());
		for (ConcreteEventLog log : selectedCandidates) {
			if (rng.nextDouble() < mutChance)
				mutatedCandidates.add(mutate(log, rng));
			else
				mutatedCandidates.add(log);
		}
		return mutatedCandidates;
	}

	private ConcreteEventLog mutate(ConcreteEventLog log, Random rng) {
		LogGreedyFactory factory = new LogGreedyFactory(log);
		ConcreteEventLog newLog = factory.generateRandomCandidate(performSafetyCheck, rng);
		return newLog;
	}

}
